package com.lanlian.cayman.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class TimeCount extends CountDownTimer {

    private TextView mTvCode;
    private Context mContext;

    public TimeCount(Context context, long millisInFuture, long countDownInterval, TextView tvCode) {
        super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        this.mTvCode = tvCode;
        this.mContext = context;
    }

    @Override
    public void onFinish() {//计时完毕时触发
        mTvCode.setText("重新获取");
        mTvCode.setClickable(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {//计时过程显示
        mTvCode.setText(String.format("%ds", millisUntilFinished / 1000));
    }

    /**
     * 开始计时
     */
    public void startTime() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                start();
            }
        }, 500);
    }

}